import 'dart:io';
import 'package:flutter/material.dart';
import 'package:palmf_hid/common/utils/utils.dart';
import 'package:get/get.dart';
import 'package:lifecycle/lifecycle.dart';
import 'package:palmf_hid/pages/main/home/home_view.dart';
import 'main_logic.dart';

class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage>
    with LifecycleAware, LifecycleMixin, WidgetsBindingObserver {
  final logic = Get.put(MainLogic());
  final state = Get.find<MainLogic>().state;
  DateTime? _lastPress;

  @override
  void onLifecycleEvent(LifecycleEvent event) {
    if (event == LifecycleEvent.active) {

    }
  }

  @override
  Widget build(BuildContext context) {
    return _buildMainScreenWidget();
  }

  _buildMainScreenWidget() {
    return PopScope<int>(
        canPop: false,
        onPopInvokedWithResult: (bool didPop, int? result) {
          if (didPop) {
            return;
          } else {
            ///拦截后退键
            ///1秒连点2次退出
            if (_lastPress == null ||
                DateTime.now().difference(_lastPress!).inSeconds > 1) {
              _lastPress = DateTime.now();
              MyToastUtil.show("confirmExit".tr);
              return;
            } else {
              exit(0);
            }
          }
        },
        child: const HomePage(),
      );
  }



  @override
  void dispose() {
    Get.delete<MainLogic>();
    super.dispose();
  }
}
